package com.molyfun.parents.activity.report.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.RepoetFromsLineListResponse;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 目标
 */
public class ReportFromsTargetAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 1001;
    private static final int TYPE_CONTENT = 1002;
    private static final int TYPE_FOTTER = 1003;
    private Context mContext;
    private int mOldChoosePosition = 0;
    private int mSelectedPosition = 0;
    private boolean isSlector = false;
    private String targetId;

    private ArrayList<RepoetFromsLineListResponse.LineListBean> mDataList;

    public ReportFromsTargetAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void initData(ArrayList<RepoetFromsLineListResponse.LineListBean> targetList) {
        this.mDataList = targetList;
        int position = 0;
        for (RepoetFromsLineListResponse.LineListBean targetBean : targetList) {
            if (targetBean.isSelector()) {
                mOldChoosePosition = position;
                mSelectedPosition = mOldChoosePosition;
            }
            position++;
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ReportFromsTargetViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_sigle_textview_item, parent, false));

    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final RepoetFromsLineListResponse.LineListBean lineListBean = mDataList.get(position);
        final ReportFromsTargetViewHolder mTargetViewHolder = (ReportFromsTargetViewHolder) holder;

        mTargetViewHolder.mTextView.setText(lineListBean.getTarget());

        if (mSelectedPosition == position) {
            lineListBean.setSelector(true);
            mTargetViewHolder.mTextView.setBackgroundColor(mContext.getResources().getColor(R.color.color_f2f2f2));
        } else {
            lineListBean.setSelector(false);
            mTargetViewHolder.mTextView.setBackgroundColor(mContext.getResources().getColor(R.color.color_white));
        }

        if(position == (getItemCount() - 1)){
            mTargetViewHolder.mLine.setVisibility(View.GONE);
        }else{
            mTargetViewHolder.mLine.setVisibility(View.VISIBLE);
        }
        mTargetViewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 切换选择的机构
                mSelectedPosition = position;
                if (reportFromsTargetAdapterCallback != null) {
                    reportFromsTargetAdapterCallback.setSelectorPosition(position,lineListBean.getTarget());
                }
                notifyDataSetChanged();
            }
        });


    }

    @Override
    public int getItemCount() {
        if (mDataList == null || mDataList.size() == 0) {
            return 0;
        }
        return mDataList.size();
    }

    class ReportFromsTargetViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.line)
        public View mLine;
        @InjectView(R.id.textView_item_course)
        public TextView mTextView;

        public ReportFromsTargetViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public void setReportFromsTargetAdapterListener(ReportFromsTargetAdapterCallback reportFromsTargetAdapterCallback) {
        this.reportFromsTargetAdapterCallback = reportFromsTargetAdapterCallback;
    }

    private ReportFromsTargetAdapterCallback reportFromsTargetAdapterCallback;

    public interface ReportFromsTargetAdapterCallback {
        void setSelectorPosition(int position,String targetName);
    }
}
